package top.lionkliu.servlet;

import lombok.extern.slf4j.Slf4j;
import top.lionkliu.entity.User;
import top.lionkliu.service.UserService;
import top.lionkliu.service.impl.UserServiceImpl;
import top.lionkliu.utils.SystemConstants;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@Slf4j
@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String code = request.getParameter("VCode");
        String identity = request.getParameter("identity");
        HttpSession session = request.getSession();
        String rand = (String) session.getAttribute("rand");

        User user1 = userService.selectByName(username);

        boolean isAdmin = user1.getFlag().equals(SystemConstants.IS_ADMIN) && identity.equals("admin");
        boolean isUser = user1.getFlag().equals(SystemConstants.IS_USER) && identity.equals("user");

        // 管理员身份登录
        if (isAdmin) {
            System.out.println("admin");
            if (code.equalsIgnoreCase(rand)) {
                User user = new User(username, password);
                Boolean login = userService.login(user);
                if (login) {
                    log.info("用户：{}登录成功", username);
                    session.setAttribute("username", username);
                    session.setAttribute("loginUser", user1);
                    session.setAttribute("identity", user1.getFlag());
                    response.sendRedirect("/admin/index.jsp");
                } else {
                    System.out.println("用户不存在或者密码错误!");
                    response.sendRedirect("/errorPage/login_error.jsp");
                }
            } else {
                System.out.println("验证码出错！！");
                response.sendRedirect("/errorPage/code_error.jsp");
            }
        } else if (isUser) {
            System.out.println("user");
            if (code.equalsIgnoreCase(rand)) {
                User user = new User(username, password);
                Boolean login = userService.login(user);
                if (login) {
                    log.info("用户：{}登录成功", username);
                    session.setAttribute("loginUser", user1);
                    session.setAttribute("identity", user1.getFlag());
                    request.getSession().setAttribute("username", username);
                    response.sendRedirect("/user/index.jsp");
                } else {
                    System.out.println("用户不存在或者密码错误!");
                    response.sendRedirect("/errorPage/login_error.jsp");
                }
            } else {
                System.out.println("验证码出错！！");
                response.sendRedirect("/errorPage/code_error.jsp");
            }

        } else {
            System.out.println("身份不一致！！");
            response.sendRedirect("/errorPage/identity_error.jsp");
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
